/**
 * QuizController
 *
 * @description :: Server-side logic for managing quizzes
 * @help        :: See http://sailsjs.org/#!/documentation/concepts/Controllers
 */

module.exports = {

	createAll: function (req, res, next) {
		var questions = req.params.all().questions;
		var quizLength = req.params.all().length
		delete req.params.all().questions;
		
		Quiz.create( req.params.all(), function (err, quiz) {
			if (err) return next(err);

			for (var i = 0; i < quizLength -1; i++){
				console.log(questions[i].answers)
				var newQuestion = {
					quiz: quiz.id,
					name: questions[i].name,
					answers: questions[i].answers,
					question_type: questions[i].question_type,
					description: questions[i].description
				}
				Question.create (newQuestion, function (err, questions) {
					if (err) return next(err);
				})
			}

			res.status(201);
			res.json(quiz);
		});
	},

	generateQr: function (req, res, next) {
		var userName = req.params.all().userName;
		var quizId = req.params.all().id;
		var voteId = req.params.all().voteId;
		console.log(req.params.all().voteId)
		console.log(req.params.all());
		var qr = require('qr-image');
		codeUrl = 'http://voting.shanghaiappmaker.com/#/' + userName + '/' + quizId + '/' + voteId;
		var code = qr.image(codeUrl, { type: 'png' });
		res.type('png');
		code.pipe(res);
	},

	updateAll: function (req, res, next) {
		var questions  = req.params.all().questions;
		delete req.params.all().questions;
		console.log(req.params.all());
		console.log(req.params.all().id);

		function updateAnswers(indexOuter, indexInner){
			QuestionChoice.update(
				{
					id: questions[indexOuter].answers[indexInner].id
				},
				{
					content: questions[indexOuter].answers[indexInner].content
				})
			.exec(function cb3(err3, updated3){
				if (err3){
					console.log(err3)
					return
				}
			})
		}

		Quiz.update ( 
			{ 
				id: req.params.all().id 
			}, 
			{ 
				name: req.params.all().name, 
				length: req.params.all().length,
				is_competition: req.params.all().is_competition,
				requires_login: req.params.all().requires_login 
			})

		.exec(function cb(err, updated){
			if (err){
				console.log(err);
				return
			}
			//update question in questions
			for (var i = 0; i < questions.length; i++){
				for (var j = 0; j < questions[i].answers.length; j++){
					updateAnswers(i, j)
				}
				Question.update (
					{
						id: questions[i].id
					},
					{
						name: questions[i].name,
					})
				.exec(function cb2(err2, updated2){
					if (err2){
						console.log(err2);
						return
					}
					//for (var j = 0; j < questions[i].answers.length; j++){
					//	console.log(questions[i])
					//}
				})
			}
			//for (var i=0; i<)
			//update answer in answers by question


			//console.log('User Succesfully updated')
			res.status(200);
			res.json(updated);

		})
	},

	createOrUpdateAll: function (req, res, next) {
		var questions  = req.params.all().questions;
		delete req.params.all().questions;

		function updateAnswers(indexOuter, indexInner, created){
			QuestionChoice.findOne({unique_id: questions[indexOuter].answers[indexInner].unique_id}).exec(function findQcCb(err, found){
				if (found){
					QuestionChoice.update(
					{
						unique_id: questions[indexOuter].answers[indexInner].unique_id
					},
					{
						content: questions[indexOuter].answers[indexInner].content
					})
					.exec(function cb3(err3, updated3){
						if (err3){
							console.log(err3)
							return
						}
					})
				} else {
					QuestionChoice.create({
						content: questions[indexOuter].answers[indexInner].content,
						unique_id: questions[indexOuter].answers[indexInner].unique_id,
						is_correct: questions[indexOuter].answers[indexInner].is_correct,
						question: created.id
					})
					.exec(function (err, created){
						if (err){console.log(err)}
					})
				}
			})
		}

		function updateQuestion(i, updated){
			Question.findOne({unique_id: questions[i].unique_id}).exec(function findCB(err, found){
					if (found){
						Question.update(
							{
								unique_id: questions[i].unique_id
							},
							{
								name: questions[i].name
							}
						)
						.exec(function updateCB(err, found){
							if (err){console.log(err)}
							for (var j = 0; j < questions[i].answers.length; j++){
								updateAnswers(i, j, found[0])
							}
						})
					} else {
						Question.create({
							quiz: updated[0].id,
							name: questions[i].name,
							//answers: questions[i].answers,
							question_type: questions[i].question_type,
							unique_id: questions[i].unique_id,
							description: questions[i].description
						})
						.exec(function (err, created){
							if (err){console.log(err)}

							for (var j = 0; j < questions[i].answers.length; j++){
								updateAnswers(i, j, created)
								//console.log('logging i from inner loop')
								//console.log(i)
								//console.log('logging j from inner loop')
								//console.log(j)

							}
						})
					}
				})
		}

		Quiz.update ( 
			{ 
				id: req.params.all().id 
			}, 
			{ 
				name: req.params.all().name, 
				length: req.params.all().length,
				is_competition: req.params.all().is_competition,
				requires_login: req.params.all().requires_login,
			})

		.exec(function cb(err, updated){
			//console.log(updated)
			if (err){
				console.log(err);
				return
			}
			//update question in questions
			for (var i = 0; i < questions.length; i++){
				updateQuestion(i, updated)
				//console.log('logging i from outer for loop')
				//console.log(i)
			}

			//for (var i=0; i<)
			//update answer in answers by question


			//console.log('User Succesfully updated')
			res.status(200);
			res.json(updated);

		})
	},

	deleteAll: function (req, res, next) {
		var quiz = req.params.all()
		for (var i = 0; i < quiz.questions.length; i++){
			console.log(quiz.questions[i].id)
			Question.destroy({id: quiz.questions[i].id})
			.exec(function cb2(err2, deleted2){
				if (err2){
					console.log(err2);
					return
				}
			})
			//for (var j = 0; j < quiz.questions[i].answers.length; j++){
			//}
		}
		Quiz.destroy({id: quiz.id})
		.exec(function cb(err, deleted){
			if (err){
				console.log(err)
				return
			}
		res.status(200);
		res.json(deleted);
		})
	}

	
};